/** 单例方式继承原型 */
// 原始方法
let singleCase = function () { }

singleCase.getInstance = (function () {
  let instance
  return function () {
    if (!instance) instance = new singleCase()
    return instance
  }
})()

let a = singleCase.getInstance()
let b = singleCase.getInstance()

console.log("a===b", a === b);


const CreateCase = (function () {
  let instance;
  return function () {
    if (instance) {
      console.log("1");
      return instance
    }
    // code ...
    return instance = this
  }
})()

let c = new CreateCase()
let d = new CreateCase()

console.log("c===d", c === d);
/**
 * 通过闭包的形式继承了实例对象，但是这样语义性不好，和 new xxx 方式获取对象不同
 * 下面可以通过代理的方式实现单例
 */

class caseY {
  constructor(ny, cb) {
    console.log("caseY");
    this.ny = ny
    this.cb = cb
  }
}
const ProxySingleCase = (function () {
  let stance
  return function () {
    if (!stance) {
      console.log("执行了新增原型");
      stance = new caseY('ni', 'bb')
    }
    return stance
  }
})()


let e = new ProxySingleCase()
let f = new ProxySingleCase()

console.log("e===f", e === f);















